#! /usr/bin/env python
# -*- coding: utf-8 -*-

from django.http import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.settings import api_settings
from restapi.utils.retcode import *
from restapi.models import Poc
from restapi.serializers import PocSerializer
from restapi.serializers import TrackSerializer
from restapi.models import Importance
from restapi.models import Department
from restapi.models import Progress
from restapi.models import PocTrack
from restapi.models import PocUser
import datetime

log = logger.get_logger(__name__)


@api_view(['POST'])
def get_poc_list(request):
    data=JSONParser().parse(request)
    try:
        user_id=data['user_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "用户不存在"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
    paginator = pagination_class()
    poc_list = Poc.objects.filter(poc_user_id=user_id).order_by("-poc_id")
    # poc_list = Poc.objects.all().order_by("-poc_id")
    page = paginator.paginate_queryset(poc_list, request)
    ret_ser = PocSerializer(page, many=True)
    return paginator.get_paginated_response(ret_ser.data)

@api_view(['GET'])
def get_all_poc_list(request):
    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
    paginator = pagination_class()
    poc_list = Poc.objects.all().order_by("-poc_id")
    page = paginator.paginate_queryset(poc_list, request)
    ret_ser = PocSerializer(page, many=True)
    return paginator.get_paginated_response(ret_ser.data)


@api_view(['POST'])
def create_poc(request):
    data = JSONParser().parse(request)
    try:
        user_id=data['user_id']
    except Exception:
        return JsonResponse(retcode({},"9999","用户不存在"),safe=True,status=status.HTTP_400_BAD_REQUEST)
    try:
        customer_name = data['customer_name']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入客户姓名"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        importance_id = data['importance_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请选择重要程度"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        project_name = data['project_name']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入项目名称"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        department_id = data['department_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请选择事业部"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        manager = data['manager']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入客户经理名称"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        solution = data['solution']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入解决方案"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        module = data['module']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入模块"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        progress_id = data['progress_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入落单情况"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        poc_status = data['poc_status']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入当前状态"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        exchange_persons = data['exchange_persons']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入交流人员"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        exchange_time = data['exchange_time']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入交流时间"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        budget = data['budget']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入预算"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        flag = data['flag']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入是否poc"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        work_load = data['work_load']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入工作量"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        instruction = data['instruction']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入说明"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        data_collection = data['data_collection']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入提交资料情况"), safe=True, status=status.HTTP_400_BAD_REQUEST)

    try:
        importance_model = Importance.objects.get(param_id=importance_id)
    except Importance.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "重要程度无匹配值"), safe=True, status=status.HTTP_404_NOT_FOUND)

    try:
        department_model = Department.objects.get(param_id=department_id)
    except Department.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "事业部无匹配值"), safe=True, status=status.HTTP_404_NOT_FOUND)
    try:
        progress_model = Progress.objects.get(param_id=progress_id)
    except Progress.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "落单情况无匹配值"), safe=True, status=status.HTTP_404_NOT_FOUND)
    try:
        poc_user =PocUser.objects.get(user_id=user_id)
    except PocUser.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "Poc信息不存在"), safe=True, status=status.HTTP_404_NOT_FOUND)

    _exchange_time = datetime.datetime.fromtimestamp(exchange_time)
    poc_model = Poc(customer_name=customer_name, importance=importance_model, project_name=project_name,
                    department=department_model, manager=manager, solution=solution, module=module,
                    progress=progress_model, poc_status=poc_status, exchange_time=_exchange_time,
                    exchange_persons=exchange_persons, last_update=datetime.datetime.now(), budget=budget, flag=flag,
                    work_load=work_load, instruction=instruction, poc_user=poc_user,data_collection=data_collection)
    try:
        poc_model.save()
    except Exception:
        return JsonResponse(retcode({}, "9999", "poc创建失败"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    return JsonResponse(retcode(PocSerializer(poc_model).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)


@api_view(['POST'])
def update_poc(request):
    data = JSONParser().parse(request)
    try:
        poc_id = data['poc_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入poc的id"), safe=True, status=status.HTTP_404_NOT_FOUND)
    try:
        customer_name = data['customer_name']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入客户姓名"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        importance_id = data['importance_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请选择重要程度"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        project_name = data['project_name']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入项目名称"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        department_id = data['department_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请选择事业部"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        manager = data['manager']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入客户经理名称"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        solution = data['solution']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入解决方案"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        module = data['module']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入模块"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        progress_id = data['progress_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入落单情况"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        poc_status = data['poc_status']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入当前状态"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        exchange_persons = data['exchange_persons']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入交流人员"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        exchange_time = data['exchange_time']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入交流时间"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        budget = data['budget']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入预算"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        flag = data['flag']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入是否poc"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        work_load = data['work_load']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入工作量"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        instruction = data['instruction']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入说明"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        data_collection = data['data_collection']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入提交资料情况"), safe=True, status=status.HTTP_400_BAD_REQUEST)

    try:
        poc_model = Poc.objects.get(poc_id=poc_id)
    except Poc.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "POC信息不存在"), safe=True, status=status.HTTP_404_NOT_FOUND)

    try:
        importance_model = Importance.objects.get(param_id=importance_id)
    except Importance.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "重要程度无匹配值"), safe=True, status=status.HTTP_404_NOT_FOUND)

    try:
        department_model = Department.objects.get(param_id=department_id)
    except Department.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "事业部无匹配值"), safe=True, status=status.HTTP_404_NOT_FOUND)
    try:
        progress_model = Progress.objects.get(param_id=progress_id)
    except Department.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "落单情况无匹配值"), safe=True, status=status.HTTP_404_NOT_FOUND)

    _exchange_time = datetime.datetime.fromtimestamp(exchange_time)

    poc_model.customer_name = customer_name
    poc_model.importance = importance_model
    poc_model.project_name = project_name
    poc_model.department = department_model
    poc_model.manager = manager
    poc_model.solution = solution
    poc_model.module = module
    poc_model.progress = progress_model
    poc_model.poc_status = poc_status
    poc_model.exchange_time = _exchange_time
    poc_model.exchange_persons = exchange_persons
    poc_model.last_update = datetime.datetime.now()
    poc_model.budget = budget
    poc_model.flag = flag
    poc_model.work_load = work_load
    poc_model.instruction = instruction
    poc_model.data_collection = data_collection

    try:
        poc_model.save()
    except Exception:
        return JsonResponse(retcode({}, "9999", "POC更新失败"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    return JsonResponse(retcode(PocSerializer(poc_model).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)


@api_view(['POST'])
def create_track(request):
    data = JSONParser().parse(request)
    try:
        poc_id = data['poc_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入POC的id"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        track_time = data['track_time']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入项目跟踪时间"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        activity = data['activity']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入活动内容"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        instruction = data['instruction']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入说明"), safe=True, status=status.HTTP_400_BAD_REQUEST)

    try:
        poc_model = Poc.objects.get(poc_id=poc_id)
    except Poc.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "Poc信息不存在"), safe=True, status=status.HTTP_404_NOT_FOUND)

    _track_time = datetime.datetime.fromtimestamp(track_time)

    track_model = PocTrack(track_time=_track_time, activity=activity, instruction=instruction, poc=poc_model)

    try:
        track_model.save()
    except Exception:
        return JsonResponse(retcode({}, "9999", "POC跟踪信息存储失败"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    return JsonResponse(retcode(TrackSerializer(track_model).data, "0000", "Success"), safe=True,
                        status=status.HTTP_200_OK)


@api_view(['POST'])
def update_track(request):
    data = JSONParser().parse(request)
    try:
        track_id = data['track_id']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入track的id"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        track_time = data['track_time']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入项目跟踪时间"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        activity = data['activity']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入活动内容"), safe=True, status=status.HTTP_400_BAD_REQUEST)
    try:
        instruction = data['instruction']
    except Exception:
        return JsonResponse(retcode({}, "9999", "请输入说明"), safe=True, status=status.HTTP_400_BAD_REQUEST)

    try:
        track_model = PocTrack.objects.get(track_id=track_id)
    except PocTrack.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "POC项目跟踪不存在"), safe=True, status=status.HTTP_404_NOT_FOUND)

    _track_time = datetime.datetime.fromtimestamp(track_time)
    track_model.track_time = _track_time
    track_model.activity = activity
    track_model.instruction = instruction
    try:
        track_model.save()
    except Exception:
        return JsonResponse(retcode({}, "9999", "POC跟踪更新失败"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    return JsonResponse(retcode(TrackSerializer(track_model).data, "0000", "Success"), safe=True,
                        status=status.HTTP_200_OK)


@api_view(['GET'])
def get_track_list(request, poc_id):
    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
    paginator = pagination_class()
    try:
        poc_model = Poc.objects.get(poc_id=poc_id)
    except Poc.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "poc不存在"), safe=True, status=status.HTTP_404_NOT_FOUND)
    track_list = PocTrack.objects.filter(poc=poc_model).order_by("track_time")
    page = paginator.paginate_queryset(track_list, request)
    ret_ser = TrackSerializer(page, many=True)
    return paginator.get_paginated_response(ret_ser.data)


@api_view(['DELETE'])
def delete_track(request, track_id):
    try:
        PocTrack.objects.get(track_id=track_id).delete()
    except PocTrack.DoesNotExist:
        return JsonResponse(retcode({}, "9999", "Poc track不存在"), safe=True, status=status.HTTP_404_NOT_FOUND)
    return JsonResponse(retcode({}, "0000", "Success"), safe=True, status=status.HTTP_200_OK)


@api_view(['DELETE'])
def delete_poc(request, poc_id):
    try:
        poc_model = Poc.objects.get(poc_id=poc_id)
        # 删除poc track
        PocTrack.objects.filter(poc=poc_model).delete()
        # 删除poc
        poc_model.delete()
    except Exception:
        return JsonResponse(retcode({}, "9999", "Poc track删除失败"), safe=True,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    return JsonResponse(retcode({}, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
